package com.scalar.chiptrack.operations.tapereel.actions;

import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.scalar.chiptrack.finance.dao.UpdateViewCostDAO;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.operations.tapereel.forms.ReelQtyForm;
import com.scalar.chiptrack.operations.tapereel.dao.TapeReelRequestDAO;
import com.scalar.chiptrack.operations.tapereel.ReelQty;
import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.ScalarException;

import java.util.HashMap;
import java.util.ArrayList;

/**
 * User: Ganesh
 * Date: Jun 6, 2007
 * Time: 7:18:08 PM
 */

public class ReelQtyAction  extends ChipTrackAction
{
   private static final String ACTION_NAME = "/reelQtyAction.do";

    public ActionForward execute ( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) throws Exception
    {

       String action = request.getParameter( "action" );
       ReelQtyForm reelQtyForm;
       HashMap info = new HashMap();
       HashMap errors = new HashMap();

       request.setAttribute ( SessionKeys.INFO_MESSAGES, info );
       request.setAttribute ( SessionKeys.ERROR_MESSAGES, errors );

       HttpSession session = request.getSession ();

       ArrayList packageList          = new ArrayList();
       ArrayList pinList              = new ArrayList();

       reelQtyForm =(ReelQtyForm) form;
       User user = ( User ) session.getAttribute ( SessionKeys.USER_KEY );

       if ( user == null )
       {
           session.setAttribute ( SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME + "?" + request.getQueryString () );
           return ( mapping.findForward ( "login" ) );
       }

        if ( "loadPackages".equalsIgnoreCase( action ) || action == null  )
        {
            try
            {
                packageList = UpdateViewCostDAO.loadPackages(null);
                pinList = UpdateViewCostDAO.loadPins(null);
            }
            catch ( ScalarException e )
            {
                e.printStackTrace();
                errors.put ( "Could not load Package And Pin values", null );
            }
            request.setAttribute ( "packagesList", packageList );
            request.setAttribute ( "pinsList", pinList );
        }
        if ( "loadReelQtys".equalsIgnoreCase( action ) )
        {
            boolean success = false;
            try
            {
                reelQtyForm.setReelQtyList(TapeReelRequestDAO.loadReelQtys ( null, reelQtyForm.getPackageName(), reelQtyForm.getPinCount()) );
                packageList     = UpdateViewCostDAO.loadPackages(null);
                pinList         = UpdateViewCostDAO.loadPins(null);
                request.setAttribute ( "packagesList", packageList );
                request.setAttribute ( "pinsList", pinList );

            }
            catch ( ScalarException se )
            {
                se.printStackTrace();
                errors.put ("Could not get Reel Quantity Details ", null);
                success = false;
            }
        }
        if ( "saveReelQtys".equalsIgnoreCase( action ) )
        {
            boolean success = false;
            try
            {
                ArrayList reelQtyList= reelQtyForm.getReelQtyList();
                if( reelQtyList.size() > 0 )
                {
                    TapeReelRequestDAO.saveReelQty(null, reelQtyList);
                    info.put( "Reel Qty saved successfully", null );
                    for ( int i =0; i < reelQtyList.size(); i++ )
                    {
                        ReelQty reelQty = (ReelQty)reelQtyList.get ( i );
                        if ( !reelQty.isExist() )
                        {
                            errors.put ( "Could not save reel qty for the combination:" + reelQty.getPackageName() + ":" + reelQty.getPinCount() + " as this combination is already exist", null );
                        }
                    }
                    success=true;
                }
                packageList     = UpdateViewCostDAO.loadPackages(null);
                pinList         = UpdateViewCostDAO.loadPins(null);
            }
            catch ( ScalarException se )
            {
                se.printStackTrace();
                errors.put ("Could not Save Reel Qty Details ", null);
            }
            request.setAttribute ( "packagesList", packageList );
            request.setAttribute ( "pinsList", pinList );
       }
        return mapping.findForward("loadReelQtyDetails");
    }

}
